package com.season.seasonStudy.csv;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;

import java.io.*;

public class CSVTest {

    /**
     * 实现遍历文件夹下的所有的csv文件。获取数据后，可以进行其他的操作，比如说存入数据库中
     * @param args
     */
    public static void main(String[] args) {
        File parentPath = new File("C:\\Users\\season\\Desktop\\gqk");
        for (File file : parentPath.listFiles()) {

            try (Reader reader = new InputStreamReader(new FileInputStream(file), "GB2312")) {
                Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(reader);
                for (CSVRecord record : records) {
                    System.out.println("Record #: " + record.getRecordNumber());
                    System.out.println(file.getName());
                    System.out.println(record.get(0));
                    System.out.println(record.get(1));
                    System.out.println(record.get(2));
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
